import { verification } from "../../utils/verification";
import { register } from "../../server/register";
import { login } from "../../utils/login";
import {baseUrl} from "../../utils/config"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: "",
    errorUsername: "",
    email: "",
    errorEmail: "",
    password1: "",
    errorPassword1: "",
    password2: "",
    errorPassword2: "",
    errorUsernameRegister: false,
    errorEmailRegister: false,
    errorPassword1Register: false,
    errorPassword2Register: false,
    avatar: baseUrl + '/imgs/avatar.png',
    isUserNameClick:false
  },
  userNameClick(){
    //获取用户微信信息
    if(!this.data.isUserNameClick){
      wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          // console.log(res);
          this.setData({
            avatar: res.userInfo.avatarUrl,
            username:  res.userInfo.nickName,
            isUserNameClick:true
          })
        }
      })
    }
  },
  inputUsername() {
    // console.log(e.detail.value);
    // this.setData({
    //   username: e.detail.value
    // })
    if (!verification.JudgmentNull(this.data.username.trim()))
     return  this.setData({
        errorUsername: "用户名不能为空",
        errorUsernameRegister: false
      })

        this.setData({
          errorUsername: "",
          errorUsernameRegister: true
        })
      
    // console.log(verification.checkUserName(this.data.username.trim()));
  },
  inputEmail() {
    // console.log(e.detail.value);
    // this.setData({
    //   email: e.detail.value
    // })
    if (!verification.JudgmentNull(this.data.email.trim())) {
      this.setData({
        errorEmail: "邮箱不能为空",
        errorEmailRegister: false
      })
    } else if (!verification.checkEmial(this.data.email.trim())) {
      this.setData({
        errorEmail: "邮箱格式不正确",
        errorEmailRegister: false
      })
    } else {
      this.setData({
        errorEmail: "",
        errorEmailRegister: true
      })
    }
  },
  inputPassword1() {
    // console.log(e.detail.value);
    // this.setData({
    //   password1: e.detail.value
    // })
    if (!verification.JudgmentNull(this.data.password1.trim())) {
      this.setData({
        errorPassword1: "密码不能为空",
        errorPassword1Register: false
      })
    } else if (!verification.checkPassword(this.data.password1.trim(), 6)) {
      this.setData({
        errorPassword1: "密码至少六位",
        errorPassword1Register: false
      })
    } else {
      this.setData({
        errorPassword1: "",
        errorPassword1Register: true
      })
    }
  },
  inputPassword2() {
    // console.log(e.detail.value);
    // this.setData({
    //   password2: e.detail.value
    // })
    if (!verification.JudgmentNull(this.data.password2.trim())) {
      this.setData({
        errorPassword2: "密码不能为空",
        errorPassword2Register: false
      })
    } else if (!verification.checkPassword(this.data.password2.trim(), 6)) {
      this.setData({
        errorPassword2: "密码至少六位",
        errorPassword2Register: false
      })
    } else if (this.data.password1 != this.data.password2) {
      this.setData({
        errorPassword2: "两次输入的密码不一致",
        errorPassword2Register: false
      })
    } else {
      this.setData({
        errorPassword2: "",
        errorPassword2Register: true
      })
    }
  },
  registerClick() {
    // errorUsernameRegister:false,
    // errorEmailRegister:false,
    // errorPassword1Register:false,
    // errorPasswordRegister:false,
    if (this.data.errorEmailRegister && this.data.errorPassword1Register && this.data.errorPassword2Register) {
      let data = {
        name: this.data.username,
        email: this.data.email,
        password: this.data.password1,
        password_confirmation: this.data.password2,
        openid:wx.getStorageSync('openid'),
        avatar:this.data.avatar
      }
      register(data).then(res => {
        // console.log(res);
        let list = {
          email: this.data.email,
          password: this.data.password1
        }
        login(list,"register")
      })
    }else{
      // this.inputUsername();
      this.inputEmail();
      this.inputPassword1();
      this.inputPassword2()
    }

  }
})